package lcr;

import java.util.Deque;
import java.util.LinkedList;

public class Test0037 {
}

class Solution0037_2 {
    public int[] asteroidCollision(int[] asteroids) {
        Deque<Integer> deque = new LinkedList<>();

        outerLoop:for (int asteroid : asteroids) {
            if (deque.isEmpty()){
                deque.offer(asteroid);
            }else {
                while (!deque.isEmpty()&&asteroid<0&&deque.peekLast()>0){
                    int oldAsteroid = deque.pollLast();
                    if (Math.abs(oldAsteroid)==Math.abs(asteroid)) continue outerLoop;
                    asteroid=Math.abs(oldAsteroid)>Math.abs(asteroid)?oldAsteroid:asteroid;
                }
                deque.offer(asteroid);
            }
        }

        int size = deque.size();
        int[] res = new int[size];
        for (int i = size-1; i >=0 ; i--) {
            res[i]=deque.pollLast();
        }
        return res;
    }
}